package com.ngu.sys.user.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ngu.sys.user.model.TSysUser;
import com.ngu.sys.user.service.TSysUserService;
import com.ngu.util.AjaxResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;


/**
 * @description: 系统用户控制层
 * @author: HSJ
 * @create: 2020-03-01 13:25
 **/
@RestController
@RequestMapping("/sys")
public class TSysUserController {
    @Autowired
    private TSysUserService tSysUserService;



    /**
    * @ClassDescription:    登录
    * @Author:         HSJ
    * @CreateDate:     2020/3/2 12:44
    */
    @PostMapping("/login")
    public AjaxResponse login(TSysUser tSysUser, HttpSession session){
        return   tSysUserService.login(tSysUser,session);
    }

    /**
    * @MethodDescription:    获取登录信息
    * @Author:      hsj
    * @CreateDate:       2020/3/2 13:11
    */
    @GetMapping("/getCurrentUserInfo")
    public AjaxResponse getCurrentUserInfo(HttpServletRequest request){
        return AjaxResponse.success("请求成功!",request.getSession().getAttribute("SysUser"));
    }


    /**
    * @MethodDescription:    查询所有系统人员信息
    * @Author:      hsj
    * @CreateDate:       2020/3/1 14:14
    */
    @GetMapping("/findSysList")
    public AjaxResponse getList(@RequestParam("page") Integer page, @RequestParam("limit") Integer limit, TSysUser t) {
        QueryWrapper<TSysUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("ACCOUNT",t.getAccount());
        Page<TSysUser> pages = new Page<>(page,limit);  // 查询第page页，每页返回limit条
        IPage<TSysUser> data = tSysUserService.page(pages,queryWrapper);
        return AjaxResponse.page("查询成功！",data.getRecords(),tSysUserService.count(queryWrapper),page);
    }

    /**
    * @MethodDescription:    新增
    * @Author:      hsj
    * @CreateDate:       2020/3/1 16:32
    */
    @PostMapping("/insertSysUser")
    public AjaxResponse insertSysUser(TSysUser t){
        Integer data = tSysUserService.insertSysUser(t);
        if(data == 1){
            return AjaxResponse.success("新增成功！",data);
        }else{
            return AjaxResponse.error("新增失败！",data);
        }
    }



    /**
    * @MethodDescription:    修改
    * @Author:      hsj
    * @CreateDate:       2020/3/1 16:44
    */
    @PostMapping("/updateSysUser")
    public  AjaxResponse update(TSysUser tSysUser){
        Integer data =  tSysUserService.updateSysUser(tSysUser);
        if(data == 1){
            return AjaxResponse.success("修改成功！",data);
        }else{
            return AjaxResponse.error("修改失败！",data);
        }
    }



    /**
    * @MethodDescription:    物理删除
    * @Author:      hsj
    * @CreateDate:       2020/3/1 16:30
    */
    @PostMapping("/delete")
    public AjaxResponse deleteById(@RequestParam("id") Long id){
        Integer data = tSysUserService.deleteById(id);
        if(data == 1){
            return AjaxResponse.success("删除成功！",data);
        }else{
            return  AjaxResponse.error("删除失败",data);
        }
    }








}